package top.codecrab.eblog.controller;

import cn.hutool.core.lang.Assert;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.codecrab.eblog.common.response.R;
import top.codecrab.eblog.entity.Comment;

/**
 * @author 王刚
 * @since 2022年03月17日 23:08
 */
@Controller
public class CommentController extends BaseController {

    /**
     * 评论
     */
    @ResponseBody
    @PostMapping("/post/reply")
    public R postReply(Long postId, String content) {
        return commentService.postReply(postId, content);
    }

    /**
     * 删除评论
     */
    @ResponseBody
    @PostMapping("/post/jieda-delete")
    public R jieDaDelete(Long id) {
        return commentService.removeComment(id);
    }

    /**
     * 采纳评论
     */
    @ResponseBody
    @PostMapping("/post/jieda-accept")
    public R jiedaAccept(Long id) {
        Comment comment = commentService.getById(id);
        Assert.notNull(comment, "找不到该评论或已被删除");
        comment.setStatus(66);
        commentService.updateById(comment);
        return R.ok();
    }

    /**
     * 评论赞
     */
    @ResponseBody
    @PostMapping("/post/jieda-zan")
    public R jiedaZan(Long id, boolean ok) {
        Comment comment = commentService.getById(id);
        Assert.notNull(comment, "找不到该评论或已被删除");
        comment.setVoteUp(comment.getVoteUp() + (ok ? -1 : 1));
        commentService.updateById(comment);
        return R.ok();
    }

}
